/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package medecins.android;


import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import modele.DAO;
import modele.Medecin;

/**
 *
 * @author bkosson
 */
public class MedecinsActivity extends ListActivity {

    private List<Medecin> lesMeds;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.medecins);
        TextView label = (TextView) findViewById(R.id.label);
        Intent inter = getIntent();
        String nom = inter.getStringExtra("leDep");
        label.setText(nom);

        lesMeds = DAO.getLesMeds(nom);
        MedecinsAdapter adapter = new MedecinsAdapter(lesMeds, this);
        setListAdapter(adapter);

        Button buttonFilter = (Button) findViewById(R.id.buttonFilter);
        buttonFilter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                EditText filtre = (EditText) findViewById(R.id.filtre);
                String val = filtre.getText().toString();
                List<Medecin> medFiltre = new ArrayList<Medecin>();
                for (Medecin m : lesMeds) {
                    if (m.getNom().startsWith(val)) {
                        medFiltre.add(m);
                    }
                }
                MedecinsAdapter adapter = new MedecinsAdapter(medFiltre, getApplicationContext());
                setListAdapter(adapter);
            }
        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String n = "tel:" + ((Medecin)l.getItemAtPosition(position)).getTel();
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(n));
        startActivity(intent);
    }
}
